package com.shelpe.services.sinbad.controller.actionLog.impl.keyword;

import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.entity.jpa.Adgroup;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.KeywordsLog;

@Service
public class KeywordDelConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertKeywrod(KeywordsLog entity, Map<Long, Campaign> campaignMap,
			Map<Long, Adgroup> adgroupMap) {
		boolean keywordDelConverter = entity.getAction() == KeywordsLog.Action.AUTO_DEL_KEYWORD
				|| entity.getAction() == KeywordsLog.Action.MANUAL_DEL_KEYWORD
				|| entity.getAction() == KeywordsLog.Action.SEMIAUTO_EXEC_DEL;
		if (keywordDelConverter) {
			ActionLogDto dto = new ActionLogDto();
			dto.from(entity);
			if (StringUtils.isNotBlank(entity.getNote())) {
				Adgroup adgroup = adgroupMap.get(entity.getAdgroupID());
				if (adgroup != null) {
					String[] params = { adgroup.getProductName(), entity.getKeyword() };
					dto.setReason(messageSource.getMessage("message.keyword.del.reason", params, Locale.ENGLISH));
					return dto;
				}
			}
		}
		return null;
	}
}
